package cn.iocoder.yudao.hires.service.impl;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.hires.service.ResumeEvaluationService;
import cn.iocoder.yudao.hires.vo.ResumeEvaluationPageReqVO;
import cn.iocoder.yudao.hires.vo.ResumeEvaluationSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.hires.entity.ResumeEvaluationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.hires.mapper.ResumeEvaluationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;


/**
 * 简历-自我评价 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ResumeEvaluationServiceImpl implements ResumeEvaluationService {

    @Resource
    private ResumeEvaluationMapper evaluationMapper;

    @Override
    public Integer createEvaluation(ResumeEvaluationSaveReqVO createReqVO) {
        // 插入
        ResumeEvaluationDO evaluation = BeanUtils.toBean(createReqVO, ResumeEvaluationDO.class);
        evaluationMapper.insert(evaluation);
        // 返回
        return evaluation.getId();
    }

    @Override
    public void updateEvaluation(ResumeEvaluationSaveReqVO updateReqVO) {
        // 校验存在
        validateEvaluationExists(updateReqVO.getId());
        // 更新
        ResumeEvaluationDO updateObj = BeanUtils.toBean(updateReqVO, ResumeEvaluationDO.class);
        evaluationMapper.updateById(updateObj);
    }

    @Override
    public void deleteEvaluation(Integer id) {
        // 校验存在
        validateEvaluationExists(id);
        // 删除
        evaluationMapper.deleteById(id);
    }

    private void validateEvaluationExists(Integer id) {
        if (evaluationMapper.selectById(id) == null) {
            throw exception(new ErrorCode(500, "简历-自我评价不存在"));
        }
    }

    @Override
    public ResumeEvaluationDO getEvaluation(Integer id) {
        return evaluationMapper.selectById(id);
    }

    @Override
    public PageResult<ResumeEvaluationDO> getEvaluationPage(ResumeEvaluationPageReqVO pageReqVO) {
        return evaluationMapper.selectPage(pageReqVO);
    }

}